package cn.kow.kmall.controller.system;

import cn.kow.kmall.admin.pojo.vo.SysUserVO;
import cn.kow.kmall.controller.BaseController;
import cn.kow.kmall.core.response.KmallResult;
import cn.kow.kmall.core.util.OSSUtils;
import cn.kow.kmall.system.entity.SysRole;
import cn.kow.kmall.system.entity.SysUser;
import cn.kow.kmall.system.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: lijun
 * @Description:
 * @Date: Created in 2019-10-29 17:04
 * @Modified By:
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class SysProfileController extends BaseController {

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/profile")
    public KmallResult profile() {
        SysUser user = currentUser();
        List<SysRole> roleList = user.getRoles();
        String roles = roleList.stream().map(role -> role.getRoleName()).collect(Collectors.joining());
        SysUserVO userVO = new SysUserVO();
        BeanUtils.copyProperties(user, userVO);
        userVO.setUserId(user.getId());
        return KmallResult.success()
                .put("user", userVO)
                .put("roleGroup", roles);
    }

    @PostMapping("/profile/update")
    public KmallResult updateProfile(@RequestBody SysUser user) {
        SysUser loginUser = currentUser();
        loginUser.setNickname(user.getNickname());
        loginUser.setPhone(user.getPhone());
        loginUser.setEmail(user.getEmail());
        loginUser.setGender(user.getGender());
        sysUserService.update(user);
        return KmallResult.success();
    }

    @PostMapping("/profile/update/pwd")
    public KmallResult updatePwd(String oldPassword, String newPassword) {
        SysUser user = currentUser();
        if (!passwordEncoder.matches(oldPassword, user.getPassword())) {
            return KmallResult.fail("修改密码失败，旧密码错误");
        }
        if (oldPassword.equals(newPassword)) {
            return KmallResult.fail("新密码不能与旧密码相同");
        }
        user.setPassword(passwordEncoder.encode(newPassword));
        sysUserService.update(user);
        return KmallResult.success();
    }

    /**
     * 头像上传
     */
    @PostMapping("/profile/avatar")
    public KmallResult avatar(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return KmallResult.fail("请选择上传图片");
        }
        String fileName = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();
        try {
            String url = OSSUtils.uploadImage("avatar", inputStream, fileName);
            SysUser user = currentUser();
            user.setAvatar(url);
            sysUserService.update(user);
            return KmallResult.success();
        } catch (Exception e) {
            return KmallResult.fail("图片上传失败");
        }
    }
}

